<template>
  <!-- 单个根元素 view 包裹所有内容 -->
  <view class="create-post-container">
    <!-- 遮罩层 -->
    <view v-if="show" class="modal-mask" @click="handleMaskClick"></view>
    
    <!-- 主弹窗 -->
    <view v-if="show" class="create-post-modal">
      <!-- 关闭按钮 -->
      <view @click="close" class="close-button">
        <view class="main-close">
          <i class="bi bi-x"></i>
        </view>
      </view>

      <!-- 内容区 - 替换 el-scrollbar 为 view -->
      <view class="modal-scroll">
        <!-- 表单内容 -->
        <view class="form-container">
          <!-- 标题 -->
          <view class="form-item">
            <input
              v-model="postForm.title"
              placeholder="输入标题（可选）"
              maxlength="30"
              class="title-input"
            />
            <text class="char-count">{{ postForm.title.length }}/30</text>
          </view>

          <!-- 正文 -->
          <textarea
            v-model="postForm.content"
            placeholder="这一刻的想法..."
            class="content-input"
            :maxlength="maxContentLength"
          ></textarea>
          <text class="char-count">{{ postForm.content.length }}/{{ maxContentLength }}</text>


          
        </view>
      </view>

      <!-- 底部操作栏 -->
      <view class="action-bar">
        <button 
          class="submit-btn" 
          :class="{ disabled: !canSubmit }"
          @click="submitPost"
        >
          <i class="bi bi-send"></i> 发布
        </button>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'CreatePost',
  props: {
    show: {
      type: Boolean,
      required: true
    }
  },
  emits: ['close', 'submit'],
  data() {
    return {
      maxContentLength: 500,
      postForm: {
		userId: 0,
        title: '',
        content: ''
      }
    }
  },
  computed: {
    canSubmit() {
      return this.postForm.content.trim().length > 0
    }
  },
  methods: {
    close() {
        this.$emit('close')
    },

    handleMaskClick() {
      this.close()
    },

    submitPost() {
      if (!this.canSubmit) return
      
      this.$emit('submit', {
		userId: uni.getStorageSync("userInfo").userId,
        title: this.postForm.title,
        content: this.postForm.content
      })
    },
  }
}
</script>

<style scoped>
/* 添加滚动区域样式 */
.modal-scroll {
  max-height: 60vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* switch 组件样式调整 */
switch {
  transform: scale(0.8);
  transform-origin: right center;
}
/* 遮罩层 */
.modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 999;
}

/* 主弹窗 */
.create-post-modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 80%;
  max-width: 500px;
  max-height: 80vh;
  background-color: #fff;
  border-radius: 12px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

/* 关闭按钮 */
.close-button {
  position: fixed;
  top: calc(50% - 23vh);  /* 根据弹窗高度计算 */
  left: calc(50% - 34vh); /* 根据弹窗宽度计算 */
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.25);
  backdrop-filter: blur(10px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1001;
  cursor: pointer;
}

.bi-x {
  font-size: 20px;
  color: #333;
}

/* 滚动区 */
.modal-scroll {
  flex: 1;
  padding: 20px;
}

/* 表单容器 */
.form-container {
  padding-bottom: 20px;
}

/* 输入框样式 */
.title-input,
.content-input {
  width: 100%;
  padding: 12px;
  border: 1px solid #eee;
  border-radius: 8px;
  font-size: 15px;
  margin-bottom: 5px;
}

.content-input {
  min-height: 150px;
}

.char-count {
  display: block;
  text-align: right;
  font-size: 12px;
  color: #999;
  margin-bottom: 15px;
}

/* 图片上传 */
.image-uploader {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin: 15px 0;
}

.image-preview {
  width: calc((100% - 20px) / 3);
  height: 100px;
  position: relative;
  border-radius: 8px;
  overflow: hidden;
}

.preview-image {
  width: 100%;
  height: 100%;
}

.remove-image {
  position: absolute;
  top: 5px;
  right: 5px;
  width: 20px;
  height: 20px;
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 12px;
}

.upload-btn {
  width: calc((100% - 20px) / 3);
  height: 100px;
  border: 1px dashed #ddd;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: #999;
}

.upload-btn .bi {
  font-size: 24px;
  margin-bottom: 5px;
}

/* 设置项 */
.setting-item {
  display: flex;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f5f5f5;
}

.setting-item .bi {
  margin-right: 10px;
  font-size: 16px;
  color: #666;
}

.setting-label {
  flex: 1;
  font-size: 14px;
}

/* 底部操作栏 */
.action-bar {
  padding: 12px 20px;
  border-top: 1px solid #f0f0f0;
}

.submit-btn {
  width: 100%;
  height: 44px;
  background-color: #5f8dee;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.submit-btn .bi {
  margin-right: 8px;
}

.submit-btn.disabled {
  background-color: #ccc;
  opacity: 0.7;
}
</style>